package com.ytc.controller;


import com.ytc.model.Good;
import com.ytc.model.Type;
import com.ytc.service.GoodService;
import com.ytc.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private TypeService typeService;

    @RequestMapping("queryGoodList")
    public String queryGoodList(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 3); //一定把分页写在上面
        List<Good> list = goodService.queryGoodList();
        PageInfo<Good> pageInfo = new PageInfo<Good>(list);
        model.addAttribute("pageInfo", pageInfo);
        System.out.println("123");
        return "good/showGood";
    }

    @RequestMapping("toAddGood")
    public String toAddGood(Model model) {
        List<Type> typelist = typeService.queryType();
        model.addAttribute("typelist", typelist);
        return "good/addGood";
    }

    @RequestMapping("addGood")
    public String addGood(Good good) {
        goodService.addGood(good);
        return "redirect:/good/queryGoodList";
    }
}
